package org.mybatis.generator.api.plus;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.TopLevelClass;

import java.io.*;
import java.util.Iterator;
import java.util.List;

/**
 * @author wangfupeng
 */
public class MybatisUtilDistinctService {

    public static void writeDistinctService(String basePackageName, String aspectPackageName, String targetProject, TopLevelClass topLevelClass, IntrospectedTable introspectedTable, String tableName, String servicePackage, String mapperPackage, String modelPackage) {
        File file = null;
        OutputStream fileOutputStream = null;
        BufferedWriter bufferedWriter = null;
        try {
            String packageNameFolder = servicePackage.replaceAll("\\.", "/");
            targetProject = targetProject.replaceAll("\\./", "");
            File folder = new File(targetProject + "/" + packageNameFolder + "/distinct");
            folder.mkdirs();
            file = new File(targetProject + "/" + packageNameFolder + "/distinct/Distinct" + tableName + "Service.java");
            //如果文件不存在，则创建文件，如果已存在，则覆盖
            file.createNewFile();
            fileOutputStream = new FileOutputStream(file);
            bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream, "utf-8"));
            bufferedWriter.write("package " + servicePackage + ".distinct;\n");
            bufferedWriter.write("" +
                    "import " + mapperPackage+".distinct.Distinct" + tableName + "Mapper;\n" +
                    "import com.gitee.fufu669.aspect.CacheMockFetchLater;\n" +
                    "import com.gitee.fufu669.aspect.CacheFetchLater;\n" +
                    "import org.springframework.beans.factory.annotation.Autowired;\n" +
                    "import org.springframework.stereotype.Service;\n" +
                    "import org.springframework.context.annotation.Lazy;\n" +
                    "\n" +
                    "import java.util.List;\n" +
                    "\n" +
                    "/*提取字段的不重复的" + "数据的服务*/\n" +
                    "@Lazy\n" +
                    "@Service\n" +
                    "public class Distinct" + tableName + "Service {\n" +
                    "    \n" +
                    "    @Autowired\n" +
                    "    Distinct" + tableName + "Mapper distinct" + tableName + "Mapper;\n" +
                    "    \n");

            /*List<IntrospectedColumn> introspectedColumns = introspectedTable.getPrimaryKeyColumns();*/
            List<IntrospectedColumn> introspectedColumns = introspectedTable.getAllColumns();
            Iterator<IntrospectedColumn> iterator = introspectedColumns.iterator();
            while (iterator.hasNext()) {
                IntrospectedColumn introspectedColumn = iterator.next();
                String columnName = introspectedColumn.getJavaProperty();
                bufferedWriter.write("" +
                        "    /*提取字段的不重复的" + introspectedColumn.getRemarks().split(":")[0] + "*/\n" +
                        "    public List getDistinct" + MybatisUtilCommon.getProperCase(columnName) + "(){\n" +
                        "        //返回结果\n" +
                        "        return distinct" + tableName + "Mapper.getDistinct" + MybatisUtilCommon.getProperCase(columnName) + "();\n" +
                        "    }\n" +
                        "\n");
            }
            bufferedWriter.write("" +
                    "    /*无缓存，根据字段名称，返回相应的不重复的值。*/\n" +
                    "    public List getDistinctWithoutCache(String field){\n");
            List<IntrospectedColumn> introspectedColumns2 = introspectedTable.getAllColumns();
            Iterator<IntrospectedColumn> iterator2 = introspectedColumns2.iterator();
            while (iterator2.hasNext()) {
                IntrospectedColumn introspectedColumn = iterator2.next();
                String columnName = introspectedColumn.getJavaProperty();
                bufferedWriter.write("        //如果字段名称相同\n");
                bufferedWriter.write("        if(\"" + MybatisUtilCommon.toLowerCaseAtFirstCharacter(columnName) + "\".equals(field)){\n" +
                        "            //返回结果\n" +
                        "            return getDistinct" + MybatisUtilCommon.getProperCase(columnName) + "();\n");
                bufferedWriter.write("        }\n");
            }
            bufferedWriter.write(
                    "" +
                            "        //返回空\n" +
                            "        return null;\n" +
                            "    }\n");
            bufferedWriter.write("\n" +
                    "    /*自动缓存，根据字段名称，返回相应的不重复的值。*/\n" +
                    "    @CacheFetchLater\n" +
                    "    public List getDistinctCacheFetchLater(String field){\n");
            bufferedWriter.write("" +
                    "        //返回结果\n" +
                    "        return getDistinctWithoutCache(field);\n" +
                    "    }\n");
            bufferedWriter.write("\n" +
                    "    /*只读缓存，根据字段名称，返回相应的不重复的值。*/\n" +
                    "    @CacheMockFetchLater\n" +
                    "    public List getDistinctCacheMockFetchLater(String field){\n");
            bufferedWriter.write("" +
                    "        //返回结果\n" +
                    "        return getDistinctWithoutCache(field);\n" +
                    "    }\n" +
                    "\n");
            bufferedWriter.write("}");
            bufferedWriter.write("\n");
            System.out.println("write to file success : " + file.getPath());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtilCommon.closeBufferWriterAndFileOutputStream(fileOutputStream, bufferedWriter);
        }
    }

}
